<template>
  <div class="sharedetail">
    <van-nav-bar
      title="分享该内容，开启赚钱之路！"
      fixed
      @click-right="clickRight"
    >
      <div slot="right">
        <img class="img" src="../../assets/detail_share.png" />
        <span class="share">分享</span>
      </div>
    </van-nav-bar>
    <div
      class="content"
      :style="{
        height: infoData.card ? 'calc(100vh - 46vw)' : 'calc(100vh - 15vw)',
      }"
    >
      <h3>{{ infoData.title }}</h3>
      <div class="con" v-html="infoData.content"></div>
      <span class="conspan">{{ infoData.copyright }}</span>
      <!-- 公众号 -->
      <div class="public" v-if="infoData.attention">
        <img :src="infoData.attentionUrl" alt="" />
      </div>
      <!-- 点赞收藏浏览量 -->
      <div class="foot">
        <span @click="like = !like">
          <i
            class="iconfont"
            :class="{ 'icon-dianzan1': like, 'icon-dianzan': !like }"
            :style="{ color: like ? 'red' : '' }"
          ></i>
          点赞
        </span>
        <span @click="collect = !collect">
          <i
            class="iconfont"
            :class="{ 'icon-shoucang1': collect, 'icon-shoucang': !collect }"
            :style="{ color: collect ? 'red' : '' }"
          ></i>
          收藏
        </span>
        <span>
          <i class="iconfont icon-liulan"></i>
          浏览 {{ infoData.score }}
        </span>
      </div>
      <van-divider />
      <!-- 提交表单 -->
      <div class="form" v-show="infoData.form && !infoData.notForm">
        <h3>填写信息 申请服务</h3>
        <div class="forms">
          <div class="item">
            <label><span>*</span>姓名</label>
            <input
              type="text"
              placeholder="您的信息将严格被保密"
              v-model="name"
            />
          </div>
          <div class="item">
            <label><span>*</span>电话</label>
            <input
              type="text"
              placeholder="您的信息将严格被保密"
              v-model="phone"
            />
          </div>
          <div class="item">
            <label><span>*</span>验证码</label>
            <input type="text" placeholder="请输入验证码" v-model="code" />
            <span class="code" @click="sendCode">{{
              isCode ? count : "获取验证码"
            }}</span>
          </div>
          <div class="item">
            <label>备注</label>
            <textarea placeholder="您的信息将严格被保密" v-model="remark" />
          </div>
        </div>
        <div class="btn" @click="addForm">提交</div>
      </div>
      <div class="forms" v-show="infoData.form && infoData.notForm">
        <img src="../../assets/images/detail/detail_form.png" alt="" />
      </div>
      <h3 class="h3">
        <img src="../../assets/images/detail/head.png" alt="" />
        文章列表
      </h3>
      <div class="list" v-if="articleArr.length != 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          @load="onLoad"
        >
          <div
            class="item"
            v-for="(item, index) in articleArr"
            :key="index"
            @click="$router.push(`/detail?id=${item.id}`)"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9 || item.type == 10"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>转发:{{ item.share }}次</span>
                <span>阅读:{{ item.score || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate.substring(0, 11) }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <van-empty
        description="当前暂无文章内容~"
        class="custom-image"
        :image="require('../../assets/dafault/noarticle.png')"
        v-else
      >
      </van-empty>

      <div class="user" v-if="infoData.card">
        <img :src="infoData.shareUserImg" class="userimg" />
        <div class="xinxi">
          <div class="rights">
            <div class="right">
              <h4>分享人：{{ infoData.shareUserName }}</h4>
              <span>
                <img src="../../assets/images/detail/ewm_y.png" alt="" />
                微信号：{{ infoData.shareWeChat }}
              </span>
              <span>
                <img src="../../assets/images/detail/ewm_y.png" alt="" />
                手机号：{{ infoData.sharePhone }}
              </span>
            </div>
            <!-- v-if="infoData.shareWeChatImage" -->
            <div class="ewm" v-if="infoData.shareWeChatImage">
              <img :src="infoData.shareWeChatImage" alt="" />
            </div>
          </div>
          <div class="foots">
            <p>长按可识别二维码</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 弹框 -->
    <div class="overlay" v-show="show"></div>
    <div class="dialog" v-show="show">
      <img
        src="../../assets/images/detail/detail_img.png"
        alt=""
        class="dialog_img"
      />
      <h3>完善个人信息</h3>
      <p>完善信息后，可以将完善的信息在 分享文章中显示。</p>
      <div class="foot">
        <button @click="perfectClick(1)">去完善</button>
        <button @click="perfectClick(2)">直接分享</button>
      </div>
      <div class="close" @click="show = false">
        <img src="../../assets/images/detail/detail_close.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import sdk from "../../plugins/wxshare";
import extract from "../../plugins/extract";

export default {
  data() {
    return {
      show: false, //弹框
      id: "",
      getPhoneObj: "", //获取微信权限信息
      infoData: {},
      articleArr: [], //文章列表
      name: "", //姓名
      phone: "", //手机号
      remark: "", //备注
      code: "", //验证码
      count: 60, //倒计时
      timer: null,
      isCode: false,
      // 微信分享
      wxShareObj: {
        showShare: false,
        title: "",
        text: "",
        img: "",
        getShareUrl: "",
        show: false,
      },
      like: false, //点赞
      collect: false, //收藏
      flag: true, //重复请求节流
      userId: "",
      type: "", //类型
      standingTime: 1, //阅读时长
      timerOne: null, //定时器
      timerTwo: null, //定时器
      loading: false, //加载状态
      finished: false, //完成
      page: 1, //页数
      limit: 8, //条数
      totalPage: "", //总页数
    };
  },
  created() {
    sdk.setTitle("文章详情");
  },

  async mounted() {
    this.timerOne = setInterval((_) => {
      this.standingTime++;
    }, 1000);
    // 分享设置
    if (!JSON.parse(sessionStorage.getItem("userWxData"))) {
      this.getPhoneObj = await extract.wxShare();
    } else {
      this.getPhoneObj = JSON.parse(sessionStorage.getItem("userWxData"));
    }

    // 文章数据
    if (this.getPhoneObj) {
      await this.gettingData();
      await this.getArticleArr();
    }
    if (!this.userId) return;
    this.updateReadTime();
    this.timerTwo = setInterval((_) => {
      this.updateReadTime();
    }, 2000);
  },
  beforeDestroy() {
    clearInterval(this.timerOne);
    clearInterval(this.timerTwo);
    this.timerOne = null;
    this.timerTwo = null;
  },
  methods: {
    //触底下一页
    onLoad() {
      if (this.page >= this.totalPage) {
        this.finished = true;
        return;
      }
      this.page++;

      this.getArticleArr();
      // 加载状态结束
      this.loading = false;
    },
    //发请求
    async updateReadTime() {
      let obj = {
        customerId: this.userId,
        standingTime: this.standingTime,
        tableId: this.$route.query.tableId,
      };

      let res = await this.$api.updateReadTime(obj);
      if (res.code !== 0) return this.$toast(res.msg);
    },
    //提交表单
    async addForm() {
      let { name, phone, remark, code } = this;
      if (name == "") return this.$toast("请输入姓名！");
      if (phone == "") return this.$toast("请填写手机号！");
      if (code == "") return this.$toast("请输入验证码！");
      let obj = {
        title: this.infoData.title,
        foundTime: this.infoData.gmtCreate,
        cover: this.infoData.cover,
        name,
        phone,
        remark,
        code,
        img: this.getPhoneObj.headimgurl,
        tableId: this.$route.query.tableId,
        tableType: 1,
        account: this.$route.query.account, //公司id
        userId: this.userId, //客户id
        shareId: this.$$route.query.shareId, //分享人id
      };
      let res = await this.$api.addForm(obj);
      if (res.code !== 0) return this.$toast(res.msg);
      this.$toast("提交成功！");
      this.$router.go(0);
    },
    //点击发送验证码
    async sendCode() {
      if (this.isCode) return;
      let { phone } = this;
      if (phone.trim() == "") return this.$toast("请填写手机号！");
      let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!reg.test(phone)) return this.$toast("请输入合法的手机号 ！");
      let res = await this.$api.sendCode({ phone });
      if (res.code !== 0) return this.$toast(res.msg);
      // 倒计时
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.isCode = true;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.isCode = false;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    //完善分享
    perfectClick(n) {
      this.show = false;
      if (n == 1) {
        this.$router.push("/personal?ids=" + this.$route.query.id);
      } else {
        this.$router.push({
          path: `/shareEdit?id=${this.$route.query.tableId}&userId=${this.userId}&tableId=${this.$route.query.tableId}`,
        });
      }
    },
    //点击分享
    async clickRight() {
      //判断是否有分享次数

      if (this.type == 9) {
        //判断是否有分享次数
        let obj = {
          id: this.$route.query.tableId,
          tableType: 1,
          type: 9,
        };
        var result = await this.$api.checkingSpreadShare(obj);
      }
      if (this.type == 9 && !result.status) {
        return this.$toast("您不能分享该文章！");
      }

      let res = await this.$api.checkUserInfo(this.userId);
      if (res.code != 0) {
        this.show = true;
        return;
      }
      this.$router.push({
        path: `/shareEdit?id=${this.$route.query.tableId}&userId=${this.userId}&tableId=${this.$route.query.tableId}`,
      });
    },
    //请求文章数据
    async gettingData() {
      this.$publicObj.getLoading(); //加载
      let userInfo = JSON.parse(sessionStorage.getItem("userWxData"));

      let obj = {
        id: this.$route.query.tableId,
        tableId: this.$route.query.tableId,
        openId: userInfo.openid,
        name: this.getPhoneObj.nickname,
        image: this.getPhoneObj.headimgurl,
        shareId: this.$route.query.shareId,
        userId: this.$route.query.userId,
        tableType: 1,
        sex: this.getPhoneObj.sex,
        city: this.getPhoneObj.city,
        province: this.getPhoneObj.province,
        account: this.$route.query.account,
      };

      let res = await this.$api.getArticleShareInfo(obj);
      this.$publicObj.closeLoading();
      if (res.code != 0) return this.$toast(res.msg);
      this.infoData = res.data;
      this.userId = res.data.userId;
      this.type = res.data.type;
      sessionStorage.setItem("changeTitle", res.data.changeTitle);
      sessionStorage.setItem("shareId", res.data.userId);
      sessionStorage.setItem("agentId", this.$route.query.account);
      sessionStorage.setItem("agentIds", res.data.userId);
      sessionStorage.setItem("description", this.infoData.description);
      sessionStorage.setItem("title", this.infoData.title);
      sessionStorage.setItem("article_img", this.infoData.cover);
    },
    //获取文章任务列表
    async getArticleArr() {
      let obj = {
        account: this.$route.query.account,
        page: this.page,
        limit: this.limit,
        openId: this.getPhoneObj.openid,
        name: this.getPhoneObj.nickname,
        img: this.getPhoneObj.headimgurl,
        tabelType: 1,
        sortType: "", //排序
        sort: "",
        status: 2,
      };
      let res = await this.$api.getArticleArr(obj);
      if (res.code !== 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.articleArr = res.page.list || [];
      } else {
        this.articleArr.push(...res.page.list);
      }
      this.totalPage = res.page.totalPage;
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  box-sizing: border-box;
  // height: calc(100vh - 215px);
  padding: 66px 15px 20px;
  position: relative;
  overflow-y: scroll;

  h3 {
    color: #141414;
    font-size: 22px;
    text-align: center;
    margin-bottom: 10px;
    font-weight: 700;
  }

  .con {
    font-size: 12px;
    color: #333333;
    line-height: 24px;
    img {
      height: 171px;
      width: 100%;
      margin: 15px 0 22px 0;
    }
  }
  .conspan {
    float: right;
    font-size: 11px;
    color: #666666;
  }
  .public {
    margin-top: 35px;
    position: relative;
    background: url("../../assets/images/detail/zzh_bg.png") no-repeat;
    background-size: 100%;
    width: 100%;
    height: 138px;
    img {
      position: absolute;
      width: 87px;
      height: 87px;
      right: 60px;
      top: 30px;
    }
  }
  .foot {
    margin-top: 20px;
    display: flex;
    display: -webkit-flex;
    align-items: center;
    font-size: 11px;
    color: #6c7592;
    justify-content: space-between;
    span {
      display: flex;
      align-items: center;
      i {
        margin-right: 4px;
      }
      &:nth-of-type(2) {
        margin-right: 155px;
      }
    }
  }
  .form {
    height: 350px;
    background-color: #fbfcff;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;

    h3 {
      background: url("../../assets/images/detail/head_g.png") no-repeat;
      background-size: contain;
      background-position: center;
      width: 178px;
      font-size: 17px;
      font-weight: 700;
      text-align: center;
      color: #000000;
      margin-top: 18px;
    }
    .forms {
      margin-top: 14px;
      padding: 0 15px;
      box-sizing: border-box;
      .item {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
        position: relative;
        .code {
          position: absolute;
          right: 6px;
          top: 6px;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 60px;
          height: 21px;
          background-color: #ff8571;
          border-radius: 5px;
          font-size: 11px;
          color: #ffffff;
        }
        label {
          font-size: 14px;
          color: #000000;
          width: 63px;
          height: 20px;
          margin-right: 24px;
          span {
            color: red;
          }
        }
        input {
          font-size: 12px;
          padding: 8px 0 9px 16px;
          width: 227px;
          box-sizing: border-box;
          height: 34px;
          background: #ffffff;
          border: 1px solid #999999;
          border-radius: 5px;
          &::placeholder {
            color: #999999;
          }
        }
        textarea {
          font-size: 12px;
          padding: 8px 0 9px 16px;
          box-sizing: border-box;
          width: 228px;
          height: 91px;
          background-color: #ffffff;
          border: 1px solid #999999;
          border-radius: 5px;
          &::placeholder {
            color: #999999;
          }
        }
      }
    }
    .btn {
      width: 100%;
      height: 40px;
      background-color: #0264d1;
      border-radius: 0px 0px 5px 5px;
      font-size: 14px;
      font-weight: 700;
      color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .forms {
    width: 100%;
    height: 270px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  /deep/.van-empty__image {
    width: 166px;
    height: 139px;
  }
  .h3 {
    font-size: 16px;
    font-weight: 700;
    color: #333333;
    letter-spacing: 1px;
    margin: 20px 0 10px;
    text-align: left;
    img {
      width: 4px;
      height: 15px;
      margin-right: 5px;
    }
  }
  .list {
    .item {
      display: flex;
      margin-top: 12px;
      .left {
        width: 90px;
        height: 90px;
        margin-right: 15px;
        position: relative;
        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
        .money {
          width: 35px;
          height: 35px;
          position: absolute;
          right: 0;
          top: 0;
          border-radius: none;
        }
      }
      .right {
        flex: 1;

        span {
          font-size: 11px;
          color: #999999;
        }
        .title {
          margin-top: 5px;
          display: block;
          font-size: 14px;
          color: #333333;
        }
        .p {
          margin-top: 3px;
          height: 30px;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
        }
        .bottom {
          width: 246px;
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
          span {
            display: flex;
            align-items: center;
          }
        }
      }
    }
  }
  .user {
    background-color: #edf1f4;
    display: flex;
    padding: 0 4vw;
    background: url("../../assets/images/detail/ewm_bg.png") no-repeat;
    background-size: 100% 100%;
    box-sizing: border-box;
    height: 140px;
    width: 345px;
    position: fixed;
    left: 15px;
    bottom: 50px;
    color: #e2e2e2;
    .userimg {
      width: 84px;
      height: 84px;
      margin: 33px 0 0 12px;
      border-radius: 5px;
    }
    .xinxi {
      display: flex;
      flex-direction: column;
      flex: 1;
      .rights {
        display: flex;
        .right {
          margin-left: 35px;
          padding-top: 44px;
          h4 {
            font-size: 11px;
            margin-bottom: 12px;
          }
          span {
            display: flex;
            align-items: center;
            font-size: 9px;
            margin-bottom: 8px;
            line-height: normal;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            img {
              width: 8px;
              height: 8px;
            }
          }
        }
        .ewm {
          height: 100%;
          display: flex;
          display: -webkit-flex;
          flex-direction: column;
          justify-content: flex-end;
          align-items: center;
          margin-left: 32px;
          img {
            margin-bottom: 1px;
            width: 43px;
          }
        }
      }
      .foots {
        width: 100%;
        margin-top: 4px;
        p {
          font-size: 8px;
          color: #ffffff;
          text-align: right;
        }
      }
    }
  }
}

.img {
  width: 14px;
  height: 14px;
  margin-right: 4px;
}
.share {
  font-size: 14px;
  color: #707070;
}
.dialog {
  position: fixed;
  z-index: 2002;
  top: 50%;
  left: 50%;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
  width: 268px;
  height: 271px;
  background: url("../../assets/images/detail/detail_bg.png") no-repeat;
  background-size: 100% 100%;
  .dialog_img {
    width: 124px;
    height: 127px;
    position: absolute;
    left: 71.5px;
    top: -47px;
    z-index: 2003;
  }
  h3 {
    font-size: 16px;
    font-weight: 700;
    color: #333333;
    margin-top: 120px;
    margin-bottom: 10px;
    text-align: center;
  }
  p {
    font-size: 14px;
    color: #666666;
    padding: 0 29px;
  }
  .foot {
    margin-top: 34px;
    padding: 0 18px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    button {
      width: 90px;
      height: 32px;
      background: linear-gradient(90deg, #fcaf36 1%, #fe774c);
      border-radius: 16px;
      color: #fff;
      border: none;
      &:last-of-type {
        border: 1px solid #fd7c49;
        color: #fd7c4a;
        background: #fff;
      }
    }
  }
  .close {
    width: 35px;
    height: 35px;
    position: absolute;
    bottom: -50px;
    left: 116px;
    z-index: 2004;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background-color: rgba(90, 90, 90, 0.5);
}
</style>
